package com.bailiban.friend.service;

import com.bailiban.friend.entity.Friend;
import com.bailiban.friend.entity.Nofriend;
import com.bailiban.friend.mapper.FriendMapper;
import com.bailiban.friend.mapper.NofriendMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhulang
 * @Classname FriendService
 * @Description FriendService
 */
@Service
public class FriendService {
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private NofriendMapper nofriendMapper;

    public int addNoFriend(String userid, String friendid) {
        Nofriend nofriend = new Nofriend();
        nofriend.setFriendid(friendid);
        nofriend.setUserid(userid);
        return nofriendMapper.insert(nofriend);
    }

    public int addFriend(String userid, String friendid) {
        //先校验此好友是否已经在我的好友列表中
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("friendid", friendid).eq("userid", userid);
        Integer count = friendMapper.selectCount(queryWrapper);
        if (count > 0) {
            //已经存在就直接返回
            return 0;
        }
        //不在的话，直接添加
        Friend friend = new Friend();
        friend.setFriendid(friendid);
        friend.setUserid(userid);
        friend.setIslike("0");
        friendMapper.insert(friend);
//我是否在对方的好友列表中
        QueryWrapper<Friend> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("friendid", userid).eq("userid", friendid);
        Integer count1 = friendMapper.selectCount(queryWrapper1);
        if (count1 > 0) {
            //我在对方的好友列表中,更新我的信息
            friend.setIslike("1");
            friendMapper.update(friend, queryWrapper);
//            修改对方的islike值
            friend.setFriendid(userid);
            friend.setUserid(friendid);
            friend.setIslike("1");
            friendMapper.update(friend, queryWrapper1);
        }
        return 1;
    }
}
